package com.cherish.quartz.calendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;

import com.cherish.quartz.job.MyJob1;

/**
 * 
 * @Author: cherish
 * @Date: 2019年10月27日 下午11:40:10
 * @Description: Calendar定时任务
 */
public class CalendarDemo {

	public static void main(String[] args) throws SchedulerException {
		// TODO Auto-generated method stub

		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler scheduler = sf.getScheduler();
		scheduler.start();

		// 定义日历
		AnnualCalendar ac = new AnnualCalendar();

		// 排除国庆节
		Calendar guoqing = new GregorianCalendar(2019, 10, 28);
		ac.setDayExcluded(guoqing, true);
		// 排除元旦
		Calendar zhongqiu = new GregorianCalendar(2020, 1, 1);
		ac.setDayExcluded(zhongqiu, true);

		// 调度器添加日历
		// String calName, Calendar calendar, boolean replace, boolean updateTriggers
		scheduler.addCalendar("holiday", ac, false, false);

		JobDetail jobDetail = JobBuilder.newJob(MyJob1.class)
				// 定时任务的信息 名称 & 分组
				.withIdentity("job1", "group1")
				// 存放的数据
				.usingJobData("author", "小红伞").usingJobData("description", "测试我的第一个定时任务").build();

		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow()
				// 前面添加的日历名称
				.modifiedByCalendar("holiday")
				// 触发器
				.withSchedule(
						// 简单触发器
						SimpleScheduleBuilder.simpleSchedule()
								// 每2秒执行一次
								.withIntervalInSeconds(2)
								// 重复次数
								.repeatForever().repeatSecondlyForTotalCount(5))
				.build();

		Date firstRuntime = scheduler.scheduleJob(jobDetail, trigger);
		System.out.println(jobDetail.getKey() + "第一次触发" + firstRuntime);

	}

}
